﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using RunAllTestsInSolutionTask;
using System.IO;
using TypeMock.ArrangeActAssert;
using Microsoft.Build.Utilities;
using System.ComponentModel;
using System.Diagnostics;

namespace TasksUnitTests
{
    [TestFixture]
    //[Isolated]
    class RunTestsWithCoverageTests
    {

        private RunTestsWithCoverage _target;
        private TaskLoggingHelper _FakeLogger;
        string PERF_DIR = @"E:\Program Files (x86)\Microsoft Visual Studio 9.0\Team Tools\Performance Tools\";

        [SetUp]
        public void Setup()
        {
            //_FakeLogger = Isolate.Fake.Instance<TaskLoggingHelper>();
            
            _target = new RunTestsWithCoverage();
            //Isolate.WhenCalled(() => _target.Log).WillReturn(_FakeLogger);
        }

        [Test]
        public void Exec_FileNameNotGiven_ReturnsFalse()
        {
            var actual = _target.Execute();
            Assert.IsFalse(actual);
        }

        [Test]
        public void Exec_FileNameNotGiven_LogProperError()
        {
            _target.Execute();
            Isolate.Verify.WasCalledWithArguments(() => _FakeLogger.LogError("", "")).Matching(
                args => (args[0] as string).Contains("Can't locate Target Program:"));
            
        }

        [Test]
        public void Exec_WrongPerformanceToolDir_ReturnsFalse()
        {
            _target.FileName = @"TestData\ecm.exe";
            _target.PerformanceToolsDir="";
            Assert.Throws<Win32Exception>(() => _target.Execute());
        }

        [Test]
        public void Exec_WrongPerformanceToolDir_LogProperError()
        {
            _target.PerformanceToolsDir = "";
            _target.FileName = @"TestData\ecm.exe";
            Assert.Throws<Win32Exception>(() => _target.Execute());
            Isolate.Verify.WasCalledWithArguments(() => _FakeLogger.LogError("", "")).Matching(
                args => (args[0] as string).Contains("Couldnt locate vsinstr.exe. Check PerformanceToolsDir Setting"));
        }

        [Test]
        public void Exec_OutputDirNotSpecified_ReturnFalse()
        {
            var fakeInstruProc = Isolate.Fake.Instance<Process>();
            Isolate.Swap.NextInstance<Process>().With(fakeInstruProc);
            _target.PerformanceToolsDir = PERF_DIR;
            _target.FileName = @"TestData\ecm.exe";
            _target.OutputDirectory = "";
            var actual = _target.Execute();
            Assert.IsFalse(actual);
        }

        [Test]
        public void Exec_NosuchFile_ReturnsFalse()
        {
            _target.PerformanceToolsDir = PERF_DIR;
            _target.FileName = @"NoFile.exe";
            _target.Parmaters = "";
            _target.OutputDirectory = @"EcmCoverage.xml";
            var actual = _target.Execute();

            Assert.IsFalse(actual);
        }

        [Test]
        public void Exec_LegalFileName_ExecuteWithCoverage()
        {
            _target.PerformanceToolsDir = PERF_DIR;
            _target.FileName = @"TestData\ecm.exe";
            _target.OutputDirectory = @"./";
            var actual = _target.Execute();

            Assert.IsTrue(actual);
            Assert.IsTrue(File.Exists("EcmCoverage.xml"));
        }

        [Test]
        public void Exec_OutputDirectoryDoesNotExists_CreatesDirectory()
        {
            if (Directory.Exists(@"Results\"))
                Directory.Delete(@"Results\");
            _target.OutputDirectory = @"Results\";
            _target.PerformanceToolsDir = PERF_DIR;
            _target.FileName = "ECM.exe";


            _target.Execute();
            Assert.IsTrue(Directory.Exists(@"Results\"));
            Directory.Delete(@"Results\");
        }        

    }
}
